package com.courier.controller;

import com.courier.pojo.Courier;
import com.courier.service.CourierService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import javax.servlet.http.HttpSession;
import java.util.List;

/**
 * @author mohan lau
 * @version 1.0
 * @ClassName: CourierController
 * @Description: TODO
 * @Date: 2021-05-08 14:23
 */
@Controller
public class CourierController {
    @Autowired
    CourierService courierService;
    // 进入前台首页
    @RequestMapping("/")
    public String toindex() {
        System.out.println("toindex*************");
        return "forward:courierindex.jsp";
    }
    // 进入后台首页
    @RequestMapping("/bgcourierindex")
    public String bgcourierindex() {
        System.out.println("bgcourierindex*************");
        return "bgcourierindex";
    }

    // 进入登录界面
    @RequestMapping("/tologin")
    public String tologin() {
        System.out.println("tologin*************");
        return "courierlogin";
    }

    // 进入注册界面
    @RequestMapping("/toregist")
    public String toregist() {
        System.out.println("toregist*************");
        return "courierregist";
    }
    // 通过电话和密码登录
    @RequestMapping("/login")
    public String login(String dtel, String dpass, Model model, HttpSession session) {
        Courier courier = courierService.getCourierByTelAndPass(dtel, dpass);
        if (courier != null) {
            model.addAttribute("message", "登陆成功");
            session.setAttribute("oncourier",courier);
            return "forward:courierindex.jsp";
        }
        model.addAttribute("message", "用户不存在");
        System.out.println("用户不存在");
        return "redirect:/tologin.action";
    }

    // 注册
    @RequestMapping("/regist")
    public String regist(Courier courier, Model model) {
        if (courier.getDdate()==null){
            courier.setDdate("未提交");
            courier.setDresult("未审核");
        }else{
            courier.setDdate("已提交");
           if (courier.getDresult()==null){
               courier.setDresult("未审核");
           }
        }
        boolean newcour = courierService.insert(courier);
        if (newcour) {
            model.addAttribute("message", "注册成功");
            return "redirect:/tologin.action";
        }
        model.addAttribute("message", "注册失败，请重新注册");
        System.out.println("注册失败，请重新注册");
        return "redirect:/toregist.action";
    }

    // 注册验证用户是否存在
    @RequestMapping("/getcourierbydtel")
    @ResponseBody
    public String getcourierbydtel(String dtel) {
        System.out.println("注册验证用户是否存在");
        Courier courier = courierService.findByTel(dtel);
        if (courier != null) {
            return "true";
        }
        return "false";
    }
    //信息展示
    @RequestMapping("/couriershow")
    public String couriershow(Model model) {
        System.out.println("进入了控制器");
        List<Courier> list=courierService.findALL();
        model.addAttribute("courier",list);
        return "bgcourierindex";
    }

    //注销
    @RequestMapping("/exit")
    public String exit( HttpSession session) {
        session.invalidate();
        return "redirect:courierindex.jsp";
    }
}
